package com.mj.mes.system.SysOrganization;

import com.mj.mes.utils.BaseBeanDao;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.map.HashedMap;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mj on 2017/3/29.
 */
@Controller
@RequestMapping("organization")
public class SysOrganizationController {
    @Autowired
    private SysOrganizationService sysOrganizationService;
    @Autowired
    private BaseBeanDao baseBeanDao;
    /**
     * 带分页查询
     *
     * @param sysOrganization
     * @param page
     * @param rows
     * @param size
     * @return
     */
    @RequestMapping("findAllWithPaging")
    @ResponseBody
    public Map<String, Object> findAllWithPaging(SysOrganization sysOrganization, Integer page, Integer rows, @RequestParam(value = "rows") Integer size) {
        Map<String, Object> map = new HashedMap();
        Page<SysOrganization> list = sysOrganizationService.findAll(new PageRequest(page - 1, size));
        int total = sysOrganizationService.findAll().size();
        map.put("total", total);
        map.put("rows", list.getContent());
        return map;
    }

    /**
     * 部门菜单
     * @return
     */
    @RequestMapping("queryAll")
    @ResponseBody
    public String findAll() throws JSONException, JsonProcessingException {
        Map listMap = new HashMap();
        ObjectMapper mapper = new ObjectMapper();
        List<SysOrganization> list = sysOrganizationService.findAll();
        List arrayList =new ArrayList();
        for ( SysOrganization sysOrganization : list){
            Object object  = sysOrganization ;
            if(sysOrganization.getSysOrganization() != null){
                object = baseBeanDao.addAttributes(sysOrganization,"_parentId",sysOrganization.getSysOrganization().getId());
            }
            arrayList.add(object);
        }
        listMap.put("rows",arrayList);
        return mapper.writeValueAsString(listMap);
    }
    /**
     *
     * @param
     * @return
     */
    @RequestMapping(value = "addOrganization", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addOrganization(SysOrganization sysOrganization, HttpServletRequest request) {
        Map<String, Object> map = new HashedMap();
        long pid = Long.parseLong(request.getParameter("organization_pid"));
        //默认状态为0
        try {
            sysOrganization.setOrganizationAvailable(0);
            sysOrganization.setSysOrganization(sysOrganizationService.findOne(pid));
            sysOrganizationService.addOrganization(sysOrganization);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "fail");
            e.printStackTrace();
        }
        return map;
    }

    /**
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> deletOrganization(String ids) {
        Map<String, String> map = new HashedMap();
        String[] idArrayList = ids.split(",");
        List<SysOrganization> list = new ArrayList<SysOrganization>();
        for (int i = 0; i < idArrayList.length; i++) {
            SysOrganization sysOrganization = new SysOrganization();
            sysOrganization.setId(Long.valueOf(idArrayList[i]));
            list.add(sysOrganization);
        }
        boolean flag = sysOrganizationService.delete(list);
        if (flag) {
            map.put("msg", "success");
        } else {
            map.put("msg", "fail");
        }
        return map;
    }

    /**
     * 跳转到修改角色界面
     *
     * @param sysOrganization
     * @param model
     * @return
     */
    @RequestMapping(value = "toUpdate", method = RequestMethod.GET)
    public String toUpdateOrganization(SysOrganization sysOrganization, Model model) {
        sysOrganization = sysOrganizationService.findOne(sysOrganization.getId());
        model.addAttribute("sysOrganization", sysOrganization);
        return "organization/organizationEdit";
    }

    /**
     *
     * @return
     */
//    @RequestMapping("queryResourceByOrganizationId")
//    @ResponseBody
//    public JSONObject findAll(SysOrganization sysOrganization) {
//        System.out.println("----"+sysOrganization.getId());
//         sysOrganization=sysOrganizationService.findOne(Long.valueOf(sysOrganization.getId()));
//        Set<productionProject> roleUrl=sysOrganization.getSysUser();
//        List<SysResource> list = sysResourceService.findAll();
//        JSONArray jsonArray = new JSONArray();
//        for (SysResource sysResource : list) {
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("id", sysResource.getId());
//            jsonObject.put("name", sysResource.getResourceName());
//            jsonObject.put("resourceType", sysResource.getResourceType());
//            if (roleUrl.contains(sysResource.getResourceUrl())){
//                jsonObject.put("checked","true");
//            }
//            if (sysResource.getSysResource() != null) {
//                jsonObject.put("_parentId", sysResource.getSysResource().getId());
//            }
//            jsonArray.add(jsonObject);
//        }
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("rows", jsonArray);
//        return jsonObject;
//    }

    /**
     * 改变角色状态
     * @param organizationIds
     * @return
     */
    @PostMapping("changeOrganizationStatus")
    @ResponseBody
    public Map<String,Object>changeOrganizationStatus(String organizationIds){
        Map<String,Object>map=new HashedMap();
        List<SysOrganization>list=new ArrayList<>();
        String[] organizationIdArry=organizationIds.split(",");
        for (int i=0;i<organizationIdArry.length;i++){
            SysOrganization sysOrganization=sysOrganizationService.findOne(Long.valueOf(organizationIdArry[i]));
            if (sysOrganization.getOrganizationAvailable()!=null){
                //0表示可用
                if (sysOrganization.getOrganizationAvailable()==0){
                    sysOrganization.setOrganizationAvailable(1);
                    //将1设置为0
                }else {
                    sysOrganization.setOrganizationAvailable(0);
                }
            }else {
                sysOrganization.setOrganizationAvailable(0);//将空设置为0，即可用
            }
            list.add(sysOrganization);
        }
        try {
            sysOrganizationService.saveInBanch(list);
            map.put("msg","success");
        } catch (Exception e) {
            map.put("msg","fail");
            e.printStackTrace();
        }
        return map;
    }
}
